﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Kaskelot.SP.Model.Contracts;
using Kaskelot.SP.Services;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;

namespace Kaskelot.SP.UI.CONTROLTEMPLATES.Kaskelot.SP.UI.Forms
{
	public partial class MaterielTypForm : BaseForm, IForm
	{
		public string Id { get; set; }
		public string ParentId { get; set; }
		public MaterielTyp Current { get; set; }
		private IList<string> CurrentTypList { get; set; }

		protected void Page_Load(object sender, EventArgs e)
		{
			using (AdminService svc = new AdminService())
			{
				try
				{
					Current = svc.GetMaterielTyp(new Guid(Id));
				}
				catch (Exception ex)
				{
					
				}

				if (Current == null)
				{
					Current = new MaterielTyp();
					CurrentTypList = new List<string>();
				}
				else
				{
					IList<string> res = new List<string>();
					var v = svc.GetUnderliggandeMaterielTyperForMaterielTyp(Current.Id);

					foreach (MaterielTyp mt in v)
					{
						res.Add(mt.Id.ToString());
					}

					CurrentTypList = res;
				}
			}

			using (LookupService svc = new LookupService())
			{
				IEnumerable<MaterielTyp> materieltyper = svc.GetMaterielTypLookupData();

				IList<ListItem> typer = new List<ListItem>();
				foreach (MaterielTyp mt in materieltyper)
				{
					if (mt.Id != Current.Id)
					{
						typer.Add(new ListItem
						{
							Text = mt.Beskrivning,
							Value = mt.Id.ToString(),
							Selected = CurrentTypList.Contains(mt.Id.ToString())
						});
					}
				}

				MaterielTypRepeater.DataSource = typer;
				MaterielTypRepeater.DataBind();
			}
		}

		public string GetItemSelected(bool selected)
		{
			if (selected)
				return "selected=\"selected\"";
			else
				return String.Empty;
		}
	}
}
